class Retype < ActiveRecord::Base
  has_many :requirements
  has_many :works
  def self.find_less_requirements(level)
    Retype.find_by_level(level).requirements.find(:all, :limit => 10, :order => "updated_at DESC")
  end
  
  def find_all_requirements(str)
    self.requirements.find(:all,:conditions =>gain_search_con(str))
  end
  
  def self.find_less_designs(level)
    designs = Retype.find_by_level(level).works.find(:all, :limit => 8,:order => "updated_at DESC")
    designs                                                        
  end
  
  def self.find_newest_designs
    designs = Work.find(:all, :limit => 4, :order => "updated_at DESC")
    designs                                            
  end
  
  def find_newest_designs
    self.works.find(:all, :limit => 4, :order => "updated_at DESC")
  end
  
  
  private
  def gain_search_con(url)
    str=url.split('-') # choice_conditions
    return '' unless  str.length==4
    unless PRICE[str[0]].nil?
      con = PRICE[str[0]]
    end
    unless PROGRESS[str[1]].nil?
      con1=PROGRESS[str[1]]
      con = con.nil? ? con1 : con + ' and ' + con1
    end
    unless BEGTIME[str[2]].nil?
      con1="created_at>'#{(Time.now-BEGTIME[str[2]].days).to_formatted_s(:db)}'"
      con = con.nil? ? con1 : con + " and " + con1 
    end
    unless ENDTIME[str[3]].nil?
      con1="adddate(created_at,interval expiration day)>'#{Time.now.to_formatted_s(:db)}' and adddate(created_at,interval expiration day)< '#{(Time.now+ENDTIME[str[3]].days).to_formatted_s(:db)}'"
      con = con.nil? ? con1 : con + " and " +con1 
    end
    con 
  end
  
end
